﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace GLDotNet
{
    public class BasicSpriteBatchShaderProgram : DisposableObject, ISpriteBatchShaderProgram
    {
        ShaderProgram program;
                
        public BasicSpriteBatchShaderProgram(GraphicsDevice graphicsDevice)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            Assembly assembly = typeof(BasicSpriteBatchShaderProgram).Assembly;
            this.program = new ShaderProgram(graphicsDevice);
            this.program.AttachShader(Shader.CompileFromStream(graphicsDevice, ShaderType.VertexShader, assembly.GetManifestResourceStream("GLDotNet.BasicSpriteBatchShader.vert")));
            this.program.AttachShader(Shader.CompileFromStream(graphicsDevice, ShaderType.FragmentShader, assembly.GetManifestResourceStream("GLDotNet.BasicSpriteBatchShader.frag")));
            this.program.BindAttribLocation(0, "inPosition");
            this.program.BindAttribLocation(1, "inColor");
            this.program.BindAttribLocation(2, "inUV");
            this.program.Link();
        }

        protected override void DisposeManagedResources()
        {
            this.program.Dispose();
            this.program = null;
        }

        protected override void DisposeUnmanagedResources()
        {
        }

        public void Use()
        {
            this.program.Use();
        }

        public void SetProjectionMatrix(ref Matrix4 projection)
        {
            this.program.SetUniformMatrix("uniProjection", ref projection);
        }

        public void SetSampler(Texture2D texture)
        {
            if (texture == null)
                throw new ArgumentNullException("texture");

            this.program.SetSampler("sampler", 0, texture);
        }
    }
}
